/* 
 * $Id: string_copy.c 112 2010-12-11 14:48:59Z hyunghwan.chung $
 *
   Copyright 2005-2009 Chung, Hyung-Hwan.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an  BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

#if defined(__MSTRING)
#include <xp/bas/mstring.h>
#elif defined(__WSTRING)
#include <xp/bas/mstring.h>
#else
#include <xp/bas/string.h>
#endif

#include <xp/bas/memory.h>
#include <xp/bas/assert.h>

xp_size_t xp_strcpy (xp_char_t* buf, const xp_char_t* str)
{
	xp_char_t* org = buf;	
	while ((*buf++ = *str++) != XP_CHAR('\0'));
	return buf - org - 1;
}

xp_size_t xp_strcpyn (xp_char_t* buf, const xp_char_t* str, xp_size_t n)
{
	xp_char_t* org = buf;
	const xp_char_t* end = str + n;

	while (str < end) {
		if ((*buf++ = *str++) == XP_CHAR('\0')) return buf - org - 1;
	}

	return n;
}

xp_size_t xp_strncpy (xp_char_t* buf, const xp_char_t* str, xp_size_t len)
{
	const xp_char_t* end = str + len;
	while (str < end) *buf++ = *str++;
	*buf = XP_CHAR('\0');
	return len;
}

xp_size_t xp_strxcpy (xp_char_t* buf, xp_size_t bsz, const xp_char_t* str)
{
	xp_char_t* p, * p2;

	p = buf;
	p2 = buf + bsz - 1;

	while (p < p2) {
		if (*str == XP_CHAR('\0')) break;
		*p++ = *str++;
	}

	if (bsz > 0) *p = XP_CHAR('\0');
	return p - buf;
}

xp_size_t xp_strxncpy (
	xp_char_t* buf, xp_size_t bsz, const xp_char_t* str, xp_size_t len)
{
	xp_size_t n;

	if (bsz <= 0) return 0;
	if ((n = bsz - 1) > len) n = len;
	xp_memcpy (buf, str, n * XP_SIZEOF(xp_char_t));
	buf[n] = XP_CHAR('\0');

	return n;

#if 0
	xp_char_t* p, * p2;
	const xp_char_t* end;

	p = buf;
	p2 = buf + bsz - 1;
	end = str + len;

	while (p < p2) {
		if (str >= end) break;
		*p++ = *str++;
	}

	if (bsz > 0) *p = XP_CHAR('\0');
	return p - buf;
#endif
}

xp_size_t xp_strxput (
	xp_char_t* buf, xp_size_t bsz, const xp_char_t* str)
{
	xp_char_t* p, * p2;

	p = buf; p2 = buf + bsz;

	while (p < p2) 
	{
		if (*str == XP_T('\0')) break;
		*p++ = *str++;
	}

	return p - buf;
}

xp_size_t xp_strxnput (
	xp_char_t* buf, xp_size_t bsz, const xp_char_t* str, xp_size_t len)
{
	xp_char_t* p, * p2; 
	const xp_char_t* end;

	p = buf; p2 = buf + bsz; end = str + len;

	while (p < p2) 
	{
		if (str >= end) break;
		*p++ = *str++;
	}

	return p - buf;
}

static const xp_char_t* scan_dollar (
	const xp_char_t* f, xp_size_t l, xp_cstr_t* ident, xp_cstr_t* dfl, int depth)
{
	const xp_char_t* end = f + l;

	XP_ASSERT (l >= 2);
	
	f += 2; /* skip ${ */ 
	if (ident) ident->ptr = f;

	while (1)
	{
		if (f >= end) return XP_NULL;
		if (*f == XP_T('}') || *f == XP_T(':')) break;
		f++;
	}

	if (*f == XP_T(':'))
	{
		if (f >= end || *(f + 1) != XP_T('=')) 
		{
			/* not := */
			return XP_NULL; 
		}

		if (ident) ident->len = f - ident->ptr;

		f += 2; /* skip := */

		if (dfl) dfl->ptr = f;
		while (1)
		{
			if (f >= end) return XP_NULL;

			else if (*f == XP_T('$') && *(f + 1) == XP_T('{'))
			{
				if (depth >= 64) return XP_NULL; /* depth too deep */

				/* XP_TODO: remove recursion */
				f = scan_dollar (f, end - f, XP_NULL, XP_NULL, depth + 1);
				if (f == XP_NULL) return XP_NULL;
			}
			else if (*f == XP_T('}')) 
			{
				/* ending bracket  */
				if (dfl) dfl->len = f - dfl->ptr;
				return f + 1;
			}
			else	f++;
		}
	}
	else if (*f == XP_T('}')) 
	{
		if (ident) ident->len = f - ident->ptr;
		if (dfl) 
		{
			dfl->ptr = XP_NULL;
			dfl->len = 0;
		}
		return f + 1;
	}

	/* this part must not be reached */
	return XP_NULL;
}

static xp_char_t* expand_dollar (
	xp_char_t* buf, xp_size_t bsz,
	const xp_cstr_t* ident, const xp_cstr_t* dfl,
	xp_strsubst_t subst, void* ctx)
{
	xp_char_t* tmp;

	tmp = subst (buf, bsz, ident, ctx);
	if (tmp == XP_NULL)
	{
		/* substitution failed */
		if (dfl->len > 0)
		{
			/* take the default value */
			xp_size_t len;

			/* TODO: remove recursion */
			len = xp_strxnsubst (buf, bsz, dfl->ptr, dfl->len, subst, ctx);
			tmp = buf + len;
		}
		else tmp = buf;
	}

	return tmp;
}

xp_size_t xp_strxnsubst (
	xp_char_t* buf, xp_size_t bsz, const xp_char_t* fmt, xp_size_t fsz,
	xp_strsubst_t subst, void* ctx)
{
	xp_char_t* b = buf;
	xp_char_t* end = buf + bsz - 1;
	const xp_char_t* f = fmt;
	const xp_char_t* fend = fmt + fsz;

	if (buf != XP_STRSUBST_NOBUF && bsz <= 0) return 0;

	while (f < fend)
	{
		if (*f == XP_T('\\'))
		{
			/* get the escaped character and treat it normally.
			 * if the escaper is the last character, treat it 
			 * normally also. */
			if (f < fend - 1) f++;
		}
		else if (*f == XP_T('$') && f < fend - 1)
		{
			if (*(f + 1) == XP_T('{'))
			{
				const xp_char_t* tmp;
				xp_cstr_t ident, dfl;

				tmp = scan_dollar (f, fend - f, &ident, &dfl, 0);
				if (tmp == XP_NULL || ident.len <= 0) goto normal;
				f = tmp;

				if (buf != XP_STRSUBST_NOBUF)
				{
					b = expand_dollar (b, end - b + 1, &ident, &dfl, subst, ctx);
					if (b >= end) goto fini;
				}
				else
				{
					tmp = expand_dollar (buf, bsz, &ident, &dfl, subst, ctx);
					b += (tmp - buf);
				}

				continue;
			}
			else if (*(f + 1) == XP_T('$')) 
			{
				/* $$ -> $. \$ is also $. */
				f++;
			}
		}

	normal:
		if (buf != XP_STRSUBST_NOBUF)
		{
			if (b >= end) break;
			*b = *f;
		}
		b++; f++;
	}

fini:
	if (buf != XP_STRSUBST_NOBUF) *b = XP_T('\0');
	return b - buf;
}

xp_size_t xp_strxsubst (
	xp_char_t* buf, xp_size_t bsz, const xp_char_t* fmt, 
	xp_strsubst_t subst, void* ctx)
{
	return xp_strxnsubst (buf, bsz, fmt, xp_strlen(fmt), subst, ctx);
}
